/*
 * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
 * Copyright (C) 2017-2019, 2023 UltrasonicMadness
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ultrasonicmadness.madhelix.utils;

import java.io.File;
import java.util.ArrayList;

public class PathUtils {
    // Get path to MadHelix.jar
    public static String getMainJarPath() {
        String mainJarPath = new File(System.getProperty("java.class.path"))
                .getAbsolutePath();

        return mainJarPath;
    }

    // Get path to directory containing MadHelix.jar
    public static String getParentDirPath() {
        String parentDirPath = new File(getMainJarPath()).getParent();
        return parentDirPath;
    }

    // Get path to directory containing SoundHelix xml files.
    public static String getStylesDirPath() {
        String stylesDirPath = getParentDirPath() + File.separator + "styles";
        return stylesDirPath;
    }

    // Get path to output directory for MIDI files.
    public static String getMidiDirPath() {
        String stylesDirPath = getParentDirPath() + File.separator + "midi";
        return stylesDirPath;
    }

    // Get the path to the history file
    public static String getHistoryPath() {
        String historyPath = getParentDirPath() + File.separator +
                "history.xml";

        return historyPath;
    }

    public static String getPrefsPath() {
        String prefsPath = getParentDirPath() + File.separator +
                "preferences.xml";

        return prefsPath;
    }

    // Get list of available SoundHelix styles
    public static String[] getStyles() {
        ArrayList<String> styleList = new ArrayList<>();
        String[] fullFileList = new File(getStylesDirPath()).list();

        try {
            for (String fileName : fullFileList) {
                // Only return files ending with the extension XML
                if (fileName.substring(fileName.length() - 4).equals(".xml")) {
                    // Remove .xml from the end of the style names
                    styleList.add(fileName.substring(0, fileName.length() - 4));
                }
            }

            styleList.sort((file1, file2) -> {
                return file1.compareTo(file2);
            });
        } catch (Exception e) {
            System.err.println("Error loading styles");
        }

        // Convert ArrayList to standard array.
        return styleList.toArray(new String[styleList.size()]);
    }
}
